/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Adapted from http://forum.codecall.net/topic/57029-simple-java-web-browser/
 * and http://schiralli.files.wordpress.com/2008/02/webbrowser.pdf
 */

package browser;

/**
 *
 * @author Tom
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.net.*;
import java.util.*;
import java.io.*;

public class Browser {
    private JFrame frame;
    private JPanel panelTop;
    private JEditorPane editor;
    private JScrollPane scroll;
    private JTextField field;
    private JButton goButton;
    private JButton backButton;
    private JButton homeButton;
    private URL url;
    private Stack<String> urlStack = new Stack<String>();

    public Browser(String title) {
        initComponents();

        //set the title of the frame
        frame.setTitle(title);

        //set the default close op of the jframe
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //set size of frame
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 
        frame.setSize((int)screenDim.getWidth(),(int)screenDim.getHeight());

        //add jpanel to north of jframe
        frame.add(BorderLayout.NORTH, panelTop);
        //panelTop.setLayout(new BorderLayout(1, 0));
        //		JPanel sendPanel = new JPanel();
		//		outputPanel.add(sendPanel, BorderLayout.SOUTH);
        //		sendPanel.setLayout(new BorderLayout(0, 0));


        //add textfield and navigation button to jpanel.
        panelTop.add(backButton);
        panelTop.add(homeButton);
        panelTop.add(field);
        field.setColumns(45);
        panelTop.add(goButton);
        
        //make it listen to enter -> set page to URL entered
        field.addKeyListener(new KeyAdapter() {
 
            public void keyPressed(KeyEvent e) {
            	int key = e.getKeyCode();

            	if (key==KeyEvent.VK_ENTER)
            	{
            		try {
            			editor.setPage(field.getText());
                        field.setText(field.getText().toLowerCase());

            		}
	                catch(IOException ioe) {
	                    JOptionPane.showMessageDialog(null, ioe);
	                }
            	}
            }
        });
        
        
        
        //add scroll pane to jframe center
        frame.add(BorderLayout.CENTER, scroll);

        
        //set the frame visible
        frame.setVisible(true);
    }//end Browser() constructor

    private void initComponents() {
        //create the JFrame
        frame = new JFrame();

        //create the JPanel used to hold the text field and button.
        panelTop = new JPanel();
        
        //sets the url
        try {
            url = new URL("file://localhost/Users/yan/Documents/Home%20Page.html");
        }
        catch(MalformedURLException mue) {
            JOptionPane.showMessageDialog(null,mue);
        }
        
        
        //create the JEditorPane
        try {
            editor = new JEditorPane(url);
            
            //set the editor pane to false.
            editor.setEditable(false);
            
            //adds a hyper link listener to make hyper links work
            editor.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
	                   try {
	                	   editor.setPage(e.getURL());
	                	   field.setText(e.getURL().toString());
	                	   urlStack.push(field.getText());
	                   	}
                    	catch(IOException ioe) {
                    		JOptionPane.showMessageDialog(null,ioe);
                    	}
                    }//end hyperlinkUpdate()
                });//end HyperlinkListener
        }
        catch(IOException ioe) {
            JOptionPane.showMessageDialog(null,ioe);
        }
        
        //create the scroll pane and add the JEditorPane to it.
        scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        //create the JTextField
        field = new JTextField();

        //set the JTextField text to the url.
        //we're not doing this on the event dispatch thread, so we need to use
        //SwingUtilities.
        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               field.setText(url.toString());
               urlStack.push(field.getText());
           }
        });

        //create the button for changing pages.
        goButton = new JButton("Go");
        
        //add action listener to the button
        goButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    editor.setPage(field.getText());
                    urlStack.push(field.getText());
                }
                catch(IOException ioe) {
                    JOptionPane.showMessageDialog(null, ioe);
                }
            }
        });
        
        //create the backButton for changing pages.
        backButton = new JButton("<- Back");
        
        //add action listener to the button
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	if(urlStack.size()<=1) return;
                try
                {
                     urlStack.pop();
                     String urlString = (String)urlStack.peek();
                     field.setText(urlString);
                     editor.setPage(urlString);
                }
                catch(IOException ioe)
                {
                	editor.setText("Error : " +e);
                	JOptionPane.showMessageDialog(null, ioe);
                }
            }
        });
        
        //create the backButton for changing pages.
        homeButton = new JButton("Home");
        
        //add action listener to the button
        homeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    editor.setPage(url);
                    urlStack.push(url.toString());
                }
                catch(IOException ioe) {
                    JOptionPane.showMessageDialog(null, ioe);
                }
            }
        });
    }//end initComponents()

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Browser("HyperCast Browser");
            }
        });
    }//end main method.
}//end Browser class